<?php
require_once 'Junction/Core/Session.php';
require_once 'Test/Mock/Coupon.php';
require_once 'Test/Mock/Email.php';
require_once 'Test/Mock/User.php';

class Test_Junction_Core_Session extends PHPUnit_Framework_TestCase {

//    public function testSaveCoupon() {
//        $session = new Junction_Core_Session();
//        $coupon = new Test_Mock_Coupon();
//        $coupon->setTitle('Save Big!!');
//        $coupon->setValue('20');
//        $this->assertTrue($session->save($coupon));
//        $this->assertTrue($coupon->getId() > 0);
//
//        $coupon->setValue('10');
//        $this->assertTrue($session->save($coupon));
//    }
//
//    public function testLoadCoupon() {
//        $session = new Junction_Core_Session();
//        $coupons = $session->loadWhere('Test_Mock_Coupon');
//        foreach ($coupons as $coupon) {
//            $coupon->setValue($coupon->getValue() - 10);
//        }
//        $this->assertTrue(isset($coupon));
//        $this->assertTrue(is_a($coupon, 'Test_Mock_Coupon'));
//    }
//
//    public function testDeleteCoupon() {
//        $session = new Junction_Core_Session();
//        $this->assertTrue($session->deleteWhere('Test_Mock_Coupon') > 0);
//    }

    public function testSaveUser() {
        $session = new Junction_Core_Session();
        $user = new Test_Mock_User();

        $user->setName('John Doe');
        $user->setPassword('password');
        $user->setCreated(time());

        $email = new Test_Mock_Email();
        $email->setAddress('johndoe@mail.com');

        $user->setEmail($email);

        $this->assertTrue($session->save($user));
        $this->assertTrue($user->getId() > 0);
        $this->assertTrue($email->getId() > 0);
        $this->assertTrue($email->getUser() > 0);
        
        $email = new Test_Mock_Email();
        $email->setAddress('johnnied@mail.com');

        $user->setEmail($email);

        $this->assertTrue($session->save($user));
        $this->assertTrue($user->getId() > 0);
        $this->assertTrue($email->getId() > 0);
        $this->assertTrue($email->getUser() > 0);

        $email->setAddress('jdoe@mail.com');
        $this->assertTrue($session->save($user));
    }
    
    public function testFetchUser() {
        $session = new Junction_Core_Session();
        $users = $session->loadWhere('Test_Mock_User');
        foreach ($users as $user) {
            $user->setPassword('foobar');
        }
        $this->assertTrue(isset($user));
        $this->assertTrue($user->getId() > 0);
        
        foreach ($user->getEmails() as $email) {
            $email->setAddress('foobar');
        }
        $this->assertTrue(isset($email));
        $this->assertTrue($email->getId() > 0);
        $this->assertTrue($user->getId() == $email->getUser());
    }

    public function testDeleteUser() {
        $session = new Junction_Core_Session();
        $clause = new Junction_Clause_Where('name = ?', array('John Doe'));
        $this->assertTrue($session->deleteWhere('Test_Mock_User', $clause) > 0);
    }
    
    public function testDeleteEmail() {
        $session = new Junction_Core_Session();
        $this->assertTrue($session->deleteWhere('Test_Mock_Email') > 0);
    }
}
?>